import env from './env'
// const host = 'http://office.strtv.cn:9002/stk/api/v1'

var app = getApp()

// 普通请求
const request = (options, callback, showLoading = true) => {
  // 简化开发，如果传入字符串则转换成 对象
  if (typeof options === 'string') {
    options = {
      url: options
    }
  }
  // 显示加载中
  if (showLoading) {
    wx.showLoading({ title: '加载中' })
  }

  // 拼接请求地址
  options.url = env.host + '/' + options.url
  var header = {
    ...options.header,
    Sessionid: app.globalData.sessionid,
    Uid: app.globalData.oa_userInfo.uid
  }
  options.header = header
  if (!options.method) {
    options.method = 'GET'
  }

  wx.request({
    url: options.url,
    data: options.data,
    header: options.header,
    method: options.method,
    success: function (res) {
      return typeof callback === 'function' && callback(res)
    },
    fail: function (error) {
      return typeof callback === 'function' && callback(error)
    }
  })

  if (showLoading) {
    wx.hideLoading()
  }
}

export default {
  request
}
